extends Node3D

@export var automatic: bool ##枪械自动化 是,否
@export var 场景_火光四溅: PackedScene 
@export var GPU粒子_枪口发光: GPUParticles3D
@export var weapon_mesh: Node3D
@export var fire_rate := 14.0 ## 射速 每秒x发
@export var recoil := 0.05 ## 后坐力
@export var weapon_damage := 10 ## 武器伤害
@export var handler_ammo: AmmoHandler
@export var TYPE_AMMO : AmmoHandler.TYPE_AMMO
@onready var timer_cooldown: Timer = $Timer_Cooldown ## 子弹发射间隔
@onready var weapon_position: Vector3 = weapon_mesh.position
@onready var ray_cast_3d: RayCast3D = $RayCast3D


func _ready() -> void:
	pass

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if automatic:
		if Input.is_action_pressed("fire"):
			if timer_cooldown.is_stopped():
				shot()
	else:
		if Input.is_action_just_pressed("fire"):
			if timer_cooldown.is_stopped():
				shot()
				
	weapon_mesh.position = weapon_mesh.position.lerp(weapon_position,delta * 10.0) #每帧 对枪后退的位置与原来的位置插值实现后坐力回弹
func shot() -> void:
	if handler_ammo.has_ammo(TYPE_AMMO):
		handler_ammo.use_ammo(TYPE_AMMO)
		GPU粒子_枪口发光.restart()
		timer_cooldown.start(1.0 / fire_rate) #射速倒数为射击间隔时间
		weapon_mesh.position.z += recoil
		print("开火")
		
		if ray_cast_3d.is_colliding():
			var collider = ray_cast_3d.get_collider()
			if collider is Enemy:
				collider.hitpoints -= weapon_damage
				print("敌人生命" + str(collider.hitpoints))
			var 场景实例_火光四溅 = 场景_火光四溅.instantiate()
			add_child(场景实例_火光四溅)
			场景实例_火光四溅.global_position = ray_cast_3d.get_collision_point()
